/*
mutations 更新state
 */
import Vue from 'vue'
import {
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORYS,
  RECEIVE_SHOPS,
  RECEIVE_USER_INFO,
  RECEIVE_SHOP_GOODS,
  RECEIVE_SHOP_RATINGS,
  RECEIVE_SHOP_SELLER,
  DECREMENT_FOOD_COUNT,
  INCREMENT_FOOD_COUNT,
  CLEAR_SHOPCAR,
  RECEIVE_SEARCH_SHOPS
} from './mutation-types'

export default {
    [RECEIVE_ADDRESS](state,{address}){
        state.address = address
    },
    [RECEIVE_CATEGORYS](state,{categorys}){
        state.categorys = categorys
    },
    [RECEIVE_SHOPS](state,{shops}){
        state.shops = shops
    },
    [RECEIVE_USER_INFO](state,{userInfo}){
        state.userInfo = userInfo
    },

    [RECEIVE_SHOP_GOODS](state,{shopGoods}){
      state.shopGoods = shopGoods
    },
    [RECEIVE_SHOP_RATINGS](state,{shopRatings}){
      state.shopRatings = shopRatings
    },
    [RECEIVE_SHOP_SELLER](state,{shopSeller}){
      state.shopSeller = shopSeller
    },
    /*
    food 的增减
     */
    [INCREMENT_FOOD_COUNT](state,{food}){
     // food 的 增减，必须用set 要不然新增的属性 没有数据绑定
      if(!food.count){
        // food.count=1
        // 用到了Vue 必须引入vue
        /*
        set api的三个属性
        第一个是 要更改的对象
        第二个是 要新增的属性名 字符串
        第三个是 要新增的属性值
        */
        Vue.set(food,'count',1)
        /*
        在购物车中增加 food
         */
        state.shopCar.push(food)
      }else{
        food.count++
      }
    },
    [DECREMENT_FOOD_COUNT](state,{food}){
      // 如果food的count减少，减到0  就会被if拦住
      if(food.count){
        food.count--
        /*
        如果小于0 就在购物车中减少 food
         */
        if(food.count==0){
          state.shopCar.splice(state.shopCar.indexOf(food),1)
        }
      }
    },
    /*
    清空购物车
     */
    [CLEAR_SHOPCAR](state){
      state.shopCar.forEach((food)=>{
        food.count=0
      });
      state.shopCar=[];
    },
    /*
    搜索的结果
     */
    [RECEIVE_SEARCH_SHOPS](state,{searchShops}){
        state.searchShops=searchShops
    }
}
